package com.hlm.temperature_regulate.WeatherUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hlm.temperature_regulate.dao.WeatherInfo;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class GetWeather {

    /**
     * 从网络中接口中获得 weather 这个方法只能获得指定日期的一个weather
     *
     * @param date
     * @return
     */
    public static WeatherInfo getWeather(String date) {
        JSONObject jsonObject = null;
//        创建httpclient
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//        准备天气接口uri 注：此地址中有变量date
        String uri =
                "https://api.jisuapi.com/weather2/query?appkey=42171fe99832851e&city=青岛&date=" + date;
        System.out.println("请求地址" + uri);
//       拿到 "request"
        HttpGet httpGet = new HttpGet(uri);
        CloseableHttpResponse response = null;
        try {
//            拿到response
            response = httpClient.execute(httpGet);
//             得到响应实体 代表响应的内容 类型是HttpEntity （响应实体）
            HttpEntity httpEntity = response.getEntity();
//             把httpEntity 转换成jsonObject
            jsonObject = JSON.parseObject(EntityUtils.toString(httpEntity));
            System.out.println("jsonobj=" + jsonObject.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
//        将jsonobject转换成WeatherInfo
        return jsonObjectToObject(jsonObject);
    }

    /**
     * 循环获得日期
     *
     * @return 规定时间段的 String[] 的日期字符串
     * @throws ParseException
     */
    public static String[] getDates() throws ParseException {
//        准备好数组
        String[] dates = new String[121];
//        日期格式化工具
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        准备日历
        Calendar cal = Calendar.getInstance();
//        设置起始日期
        cal.setTime(dateFormat.parse("2020-01-01"));
        int i = 0;
//        当没到5月1号时
        while (cal.getTime().before(dateFormat.parse("2020-05-01"))) {
//            把日期转成String 放到[] 里
            dates[i] = dateFormat.format(cal.getTime());
            i++;
//            day +1
            cal.add(Calendar.DAY_OF_MONTH, 1);
        }

        return dates;
    }

    /**
     * 把JSONObject 中需要的数据放到实体类上
     *
     * @param jsonObject
     * @return
     */
    public static WeatherInfo jsonObjectToObject(JSONObject jsonObject) {
        WeatherInfo weather = new WeatherInfo();
        System.out.println("result=" + jsonObject.getJSONObject("result"));
//        这个jsonobject 数据 有两层  所以要先拿到 result
        JSONObject result = jsonObject.getJSONObject("result");
//        然后getresult 放到weather对应的属性里
        weather.setDate(result.getSqlDate("date"));
        weather.setDayTemperature(result.getString("temphigh"));
        weather.setNightTemperature(result.getString("templow"));
//        weather.setCity(result.getString("cityname"));
//        System.out.println("sqldate"+result.getSqlDate("date"));
        System.out.println("weather=" + weather.toString());
        return weather;
    }
}
